<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-9-3
 * Time: 下午5:18
 * To change this template use File | Settings | File Templates.
 */
class messages extends MY_Model
{
    /**
     * 保存留言
     *
     * @param array $data
     * @return mixed
     */
    public function save(array $data)
    {
        $info = array(
            'username' => $data['username'],
            'content' => $data['content'],
            'ip' => $data['ip'],
            'create_time' => date('Y-m-d H:i:s'),
        );

        $this->db->insert('message', $info);
        return $this->db->insert_id();
    }

    /**
     * 获取留言
     *
     * @param string $field
     * @param int $limit
     * @param int $offset
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getMessage($field = '*', $limit = 20, $offset = 0, $where = null, $order = null)
    {
        $this->db->select($field)->from('message');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        return $this->db->get()->result_array();
    }

    /**
     * 获取留言数量
     *
     * @param null $where
     * @return mixed
     */
    public function getMessageCount($where = null)
    {
        $this->db->select('*')->from('message');
        $where && $this->db->where($where);

        return $this->db->count_all_results();
    }
}
